package com.wlyuan.open.remote.consumer;

import cn.hutool.core.collection.CollectionUtil;
import com.wlyuan.open.domain.PushBusinessType;
import com.wlyuan.open.domain.PushTask;
import com.wlyuan.open.domain.assembler.PushTaskAssembler;
import com.wlyuan.open.domain.repository.PushTaskRepository;
import com.wlyuan.open.domain.repository.PushTenantRepository;
import com.wlyuan.open.remote.converter.TrackMessageConverter;
import com.wlyuan.open.remote.message.TrackMessageOrderDetailVO;
import com.wlyuan.open.vo.push.track.TrackDetailVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * 轨迹信息-出发
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class TrackConsumer {

    private final TrackMessageConverter messageConverter = new TrackMessageConverter();
    private final PushTenantRepository pushTenantRepository;
    private final PushTaskRepository pushTaskRepository;

    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = "queue.open.pusher.track"),
                    exchange = @Exchange(value = "exchange.topic.ere"), key = "orderEvent")
    })
    public void receiveTrack(String message) {
        var track = messageConverter.convert(message);
        var tenantId = track.getTenantId();
        var pushBusinessType = PushBusinessType.fromName(track.getType());
        if(pushBusinessType!=null) {
            var pushTenant = pushTenantRepository.ofTenantAndType(tenantId, pushBusinessType);
            if(pushTenant!=null){
                List<PushTask> pushTasks = new ArrayList<>(1);
                //如果是超长停留，低电量，同一个租户，就发送多条数据
                if(pushBusinessType.equals(PushBusinessType.TRACK_LONG_STAY)||pushBusinessType.equals(PushBusinessType.TRACK_LOW_BATTERY)){
                    if(CollectionUtil.isNotEmpty(track.getOrderDetails())){
                        for(TrackMessageOrderDetailVO orderDetailVO:track.getOrderDetails()){
                            if(orderDetailVO.getTenantId().equals(tenantId)){
                                var trackDetail = new TrackDetailVO();
                                BeanUtils.copyProperties(track,trackDetail);
                                trackDetail.setOrderId(orderDetailVO.getOrderId());
                                trackDetail.setContractNo(orderDetailVO.getContractNo());
                                pushTasks.add(PushTaskAssembler.merge(trackDetail,pushTenant));
                            }
                        }
                    }
                }else{
                    var trackDetail = new TrackDetailVO();
                    BeanUtils.copyProperties(track,trackDetail);
                    PushTask pushTask = PushTaskAssembler.merge(trackDetail,pushTenant);
                    pushTasks.add(pushTask);
                }
                if(pushTasks.size()>0){
                    pushTaskRepository.save(pushTasks);
                }
            }
        }
    }


}
